package com.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class FDate {
    private final String pattern = "yyyy-MM-dd";

    private final String pattern1 = "yyyyMMdd";

    private SimpleDateFormat df;

    private SimpleDateFormat df1;

    // @Constructor
    public FDate()
    {
        df = new SimpleDateFormat(pattern);
        df1 = new SimpleDateFormat(pattern1);
    }

    /**
     * 克隆FDate对象 2005－04－15 朱向峰添加
     * @return Object
     * @throws CloneNotSupportedException
     */
    public Object clone() throws CloneNotSupportedException
    {
        FDate cloned = (FDate) super.clone();
        return cloned;
    }

    // @Method
    /**
     * 输入符合格式要求的日期字符串，返回日期类型变量
     * <p>
     * <b>Example: </b>
     * <p>
     * <p>
     * getDate("2002-10-8") returns "Tue Oct 08 00:00:00 CST 2002"
     * <p>
     * @param dateString 日期字符串
     * @return 日期类型变量
     */
    public synchronized Date getDate(String dateString)
    {
        Date tDate = null;
        try
        {
            if (dateString.indexOf("-") != -1)
            {
                tDate = df.parse(dateString);
            }
            else
            {
                tDate = df1.parse(dateString);
            }
        }
        catch (Exception e)
        {
            log.info(e.toString());
        }

        return tDate;
    }

    /**
     * 输入日期类型变量，返回日期字符串
     * <p>
     * <b>Example: </b>
     * <p>
     * <p>
     * getString("Tue Oct 08 00:00:00 CST 2002") returns "2002-10-8"
     * <p>
     * @param mDate 日期类型变量
     * @return 日期字符串
     */
    public synchronized String getString(Date mDate)
    {
        String tString = null;
        if (mDate != null)
        {
            tString = df.format(mDate);
        }
        return tString;
    }
}
